package edu.jiefengli.course_selection_system.controller;

import edu.jiefengli.course_selection_system.pojo.entity.Result;
import edu.jiefengli.course_selection_system.pojo.entity.User;
import edu.jiefengli.course_selection_system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        System.out.println(user);
        User u = userService.login(user);
        if (u == null || !Objects.equals(u.getPassword(), DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))) {
            return Result.error(400, "用户名或密码错误");
        }
        // TODO: 登录逻辑
        u.setPassword("***");
        return Result.success("登录成功", u);
    }
}